
package com.fly.demo.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;

import com.ketayao.ketacustom.entity.Idable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "用户数据对象")
@Entity
@Table(name = "user")
public class User implements Idable<Long>
{
    @ApiModelProperty(hidden = true)
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    
    /**
     * 年龄
     */
    @ApiModelProperty(value = "年龄", required = true, example = "18")
    @NotNull(message = "年龄不能为空")
    @Range(min = 10, max = 80, message = "年龄必须在{min}-{max}")
    @Column(name = "age", length = 10)
    private Integer age;
    
    /**
     * 姓名
     */
    @ApiModelProperty(value = "姓名", required = true, example = "青哥")
    @NotBlank
    @Column(name = "name", length = 255)
    private String name;
    
    /**
     * 用户名
     */
    @ApiModelProperty(value = "用户名", required = true, example = "xiaomin")
    @NotBlank(message = "username不能为空")
    @Column(name = "username", nullable = false, length = 20)
    private String username;
    
    /**
     * 密码
     */
    @ApiModelProperty(value = "密码", required = true, example = "password123456")
    @NotBlank(message = "password不能为空")
    @Column(name = "password", nullable = false, length = 20)
    private String password;
    
    /**
     * 是否启用
     */
    @ApiModelProperty(value = "是否启用", example = "1")
    @Column(name = "enabled", length = 1)
    private Boolean enabled;
    
    /**
     * 创建时间
     */
    @ApiModelProperty(hidden = true)
    @Column(name = "create_date", length = 19)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createDate;
    
    /**
     * 备注
     */
    @ApiModelProperty(value = "备注, json数组", example = "{\"name\":\"青哥\",\"password\":\"password123456\",\"username\":\"小宜\"}")
    @Column(name = "comment", length = 200)
    private String comment;
    
    @PrePersist
    public void initCreation()
    {
        if (getCreateDate() == null)
        {
            setCreateDate(new Date());
        }
    }
    
    @PreUpdate
    public void initModify()
    {
        if (getCreateDate() == null)
        {
            setCreateDate(new Date());
        }
    }
}
